package features.advance.leetcode.tree.easy;

import features.advance.leetcode.Solution;
import features.advance.leetcode.tree.model.TreeNode;
import features.advance.leetcode.util.TreeUtil;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 *  100. 相同的树
 * 给你两棵二叉树的根节点 p 和 q ，编写一个函数来检验这两棵树是否相同。
 *
 * 如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。
 *
 *
 *
 * 示例 1：
 *
 *
 * 输入：p = [1,2,3], q = [1,2,3]
 * 输出：true
 * 示例 2：
 *
 *
 * 输入：p = [1,2], q = [1,null,2]
 * 输出：false
 * 示例 3：
 *
 *
 * 输入：p = [1,2,1], q = [1,1,2]
 * 输出：false
 *
 *
 * 提示：
 *
 * 两棵树上的节点数目都在范围 [0, 100] 内
 * -104 <= Node.val <= 104
 *
 * @author LIN
 * @date 2021-04-21
 *
 */
public class Solution100 extends Solution {

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null ^ q == null) {
            return false;
        }else{
            if(p == null && q == null){
                return true;
            }
            return p.getVal() == q.getVal() && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }
    }

    public static void main(String[] args) {
        String str = "[1,2,3]";
        String str1 = "[1,2,3]";
        TreeNode treeNode = TreeUtil.parseArray(str);
        TreeNode treeNode1 = TreeUtil.parseArray(str1);
        boolean sameTree = new Solution100() {
            @Override
            public boolean isSameTree(TreeNode p, TreeNode q) {
                if (p == null && q == null) {
                    return true;
                } else if (p == null || q == null) {
                    return false;
                } else if (p.getVal() != q.getVal()) {
                    return false;
                } else {
                    return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
                }
            }
        }.isSameTree(treeNode, treeNode1);
        System.out.println(sameTree);
    }

}
